﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form4 : Form
    {
       

        public Form4(int id)
        {
            InitializeComponent();
        }
        
        public Form4(int id,string Username, string password)
        {
             InitializeComponent();
            this.id = id;
            textBox1.Text = Username.ToString();
            textBox2.Text = password.ToString();
            

        }

        public Form4()
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var pwd = textBox2.Text;
            //更新哦呢
            if (id > 0)
            {

                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("update Users set Username='{0}',Password={1}where Id={2}", name, pwd, this.id);
                    var res = Dbhelper.AddOrEditOrDeleSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
               
            }//新增
            else
            {   
              


                

                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("insert into Users(Username,Password) values ('{0}','{1}')", name,pwd);
                    var res = Dbhelper.AddOrEditOrDeleSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult=DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("学生姓名不能为空", "提示");
                }
            }
        }
          private int id;
        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();

        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }
    }
}
